// info-query.js

document.addEventListener('DOMContentLoaded', function () {
    // 职位查询表单提交
    const jobForm = document.getElementById('job-query-form');
    const jobResults = document.getElementById('job-results');

    if (jobForm) {
        jobForm.addEventListener('submit', async function (e) {
            e.preventDefault();
            // 获取表单数据
            const formData = new FormData(jobForm);
            const params = new URLSearchParams();
            for (const [key, value] of formData.entries()) {
                if (value) params.append(key, value);
            }
            // 请求后端接口
            jobResults.innerHTML = '<div style="padding:20px;">查询中...</div>';
            try {
                const res = await fetch(`http://localhost:3001/api/jobs/search?${params.toString()}`);
                const data = await res.json();
                if (data.success && data.data.jobs.length > 0) {
                    renderJobResults(data.data.jobs);
                } else {
                    jobResults.innerHTML = '<div style="padding:20px; color:#888;">未查询到相关职位</div>';
                }
            } catch (err) {
                jobResults.innerHTML = '<div style="padding:20px; color:red;">查询失败，请稍后重试</div>';
            }
        });
    }

    function renderJobResults(jobs) {
        let html = '<div class="result-header">'
            + '<span>职位名称</span>'
            + '<span>公司</span>'
            + '<span>薪资</span>'
            + '<span>地区</span>'
            + '<span>发布时间</span>'
            + '</div>';
        html += jobs.map(job => {
            return `<div class="result-item">
                <div class="result-info">
                    <div class="result-title">${job.title}</div>
                    <div class="result-meta">${job.industry || ''} | ${job.experience || ''} | ${job.education || ''}</div>
                    <div style="color:#888;font-size:0.95em;max-width:400px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${job.description ? job.description.substring(0, 60) : ''}</div>
                </div>
                <div style="flex:0 0 120px;align-self:center;">${job.company ? job.company.name : ''}</div>
                <div style="flex:0 0 100px;align-self:center;">${job.salaryRange || ''}</div>
                <div style="flex:0 0 100px;align-self:center;">${job.location || ''}</div>
                <div style="flex:0 0 120px;align-self:center;">${job.createdAt ? job.createdAt.substring(0,10) : ''}</div>
            </div>`;
        }).join('');
        jobResults.innerHTML = html;
    }
}); 